package com.ajie.bluetoothcard.util;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DateUtil {
	
	static SimpleDateFormat sdf2,sdf;
	/**
	 * 2013-05-16
	 * @param dateStr
	 * @return
	 */
	public static String DetailedTime(String dateStr){
		try {
			sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			long currentMillis = sdf2.parse(dateStr).getTime();
			Date date = new Date(currentMillis);
			sdf = new SimpleDateFormat("yyyy-MM-dd");
			String fmt = sdf.format(date);
			return fmt;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 20130516
	 * @param dateStr
	 * @return
	 */
	public static String formatTimeLong(String dateStr){
		try {
			sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			long currentMillis = sdf2.parse(dateStr).getTime();
			Date date = new Date(currentMillis);
			sdf = new SimpleDateFormat("yyyyMMdd");
			String fmt = sdf.format(date);
			return fmt;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 0516
	 * @param dateStr
	 * @return
	 */
	public static String formatTimeShort(String dateStr){
		try {
			sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			long currentMillis = sdf2.parse(dateStr).getTime();
			Date date = new Date(currentMillis);
			sdf = new SimpleDateFormat("MMdd");
			String fmt = sdf.format(date);
			return fmt;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	/**
	 * 2013-11-03 11:09:23
	 * @param currentMillis
	 * @return
	 */
	public static String createTime(long currentMillis) {
		Date date = new Date(currentMillis);
		sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String fmt = sdf.format(date);
		return fmt;
	}
	
	/**
	 * 1384477920000
	 * @param time (格式可能是2013-11-03 11:09 也可能是2013-11-03)
	 * @return
	 */
	public static String formatCurrentMillis(String time){
		long currentMillis = 0 ;
		if(time.length()>10){
			sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		}else{
			sdf2 = new SimpleDateFormat("yyyy-MM-dd");
		}
		try {
			currentMillis = sdf2.parse(time).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return currentMillis+"";
	}
}
